﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyFinanceManager.Objects;

namespace MyFinanceManager.PC
{
    /// <summary>
    /// Interaction logic for ucAddNewFuel.xaml
    /// </summary>
    public partial class ucAddNewFuel : UserControl
    {
        public ucAddNewFuel()
        {
            InitializeComponent();

            foreach (var item in Database.NamesOfFuelstations)
            {
                this.Stations.Items.Add(item);
            }
        }
        public FuelRecord Value
        {
            get
            {
                ObjectDataProvider odp = this.FindResource("FuelRecordDS") as ObjectDataProvider;
                FuelRecord pr = odp.Data as FuelRecord;
                return pr;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            BindingOperations.GetBindingExpressionBase(this.tbAmountOfGas, TextBox.TextProperty).UpdateSource();
            BindingOperations.GetBindingExpressionBase(this.tbGasprice, TextBox.TextProperty).UpdateSource();

            ObjectDataProvider odp = this.FindResource("FuelRecordDS") as ObjectDataProvider;
            FuelRecord pr = odp.Data as FuelRecord;
            if (pr.Gasprice > 0)
            {
                pr.AmountOfGas = pr.Amount / pr.Gasprice;
                this.tbAmountOfGas.Text = pr.AmountOfGas.ToString("N2");
                Console.WriteLine(String.Format("LostFocus: {1:N2}/{2:N3}={0:N2}", pr.AmountOfGas, pr.Amount, pr.Gasprice));
            }
        }
    }
}
